package com.cy.pj.eiems.service.impl;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.cy.pj.eiems.Exception.ServiceException;
import com.cy.pj.eiems.dao.SysStudentDao;
import com.cy.pj.eiems.entity.SysStudent;
import com.cy.pj.eiems.service.SysStudentService;


@Service
public class SysStudentServiceImpl implements SysStudentService{
	@Autowired
	private SysStudentDao sysStudentDao;
	@Override
	public List<Map<String, Object>> findObjects() {
		List<Map<String, Object>> list = sysStudentDao.findObjects();
		//验证数组是否合法
		if(list==null||list.size()==0)
			throw new ServiceException("没有对应的菜单信息");
		return list;
	}
	@Override
	public int deleteStudentObject(Integer id) {
		//1.验证数据的合法性
		if(id==null||id<=0)
			throw new IllegalArgumentException("请先选择");
		//2.删除学生元素
		int rows = sysStudentDao.deleteStudentObject(id);
		if(rows==0)
			throw new ServiceException("该学生基本信息可能已经不存在");	
		return rows;
	}
	@Override
	public int saveStudentObject(SysStudent entity) {
		//1.合法验证
		if(entity==null)
			throw new ServiceException("保存对象不能为空");
		if(StringUtils.isEmpty(entity.getUsername()))
			throw new ServiceException("用户名不能为空");
		int rows;
		//2.保存数据
		try{
			rows=sysStudentDao.insertStudentObject(entity);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("保存失败");
		}
		//3.返回数据
		return rows;
	}
	@Override
	public int updateStudentObject(SysStudent entity) {
		//1.合法验证
		if(entity==null)
			throw new ServiceException("保存对象不能为空");
		if(StringUtils.isEmpty(entity.getUsername()))
			throw new ServiceException("用户名不能为空");
		//2.更新数据
		int rows=sysStudentDao.updateStudentObject(entity);
		if(rows==0)
			throw new ServiceException("记录可能已经不存在");
		//3.返回数据
		return rows;
	}
	@Override
	public int validById(Integer id, 
			             Integer valid) {
		//1.合法性验证
		if(id==null||id<=0)
			throw new ServiceException("参数不合法,id="+id);
		if(valid!=1&&valid!=0) 
			throw new ServiceException("参数不合法,valie="+valid);
		//2.执行禁用或启用操作
		int rows=0;
		try{
			rows=sysStudentDao.validById(id, valid);
		}catch(Throwable e){
			e.printStackTrace();
			//报警,给维护人员发短信
			throw new ServiceException("底层正在维护");
		}
		//3.判定结果,并返回
		if(rows==0)
			throw new ServiceException("此记录可能已经不存在");
		return rows;
	}
}
